import { Component, Input, Output, EventEmitter, OnInit, OnChanges } from "@angular/core";
import { NzNotificationService, UploadFile } from "ng-zorro-antd";
import { Router } from "@angular/router";
import { HttpServcie } from "../../../service/http.service";
import { Config } from "../../../service/config";
import { el } from "@angular/platform-browser/testing/src/browser_util";
import {FormBuilder, FormGroup, Validators, FormControl} from "@angular/forms";
import {AdvertisementBean} from "../bean/advertisement.bean";
import { observable } from "rxjs/symbol/observable";
import {InformationBean} from "../../user/information/bean/information.bean";

@Component({
  selector: "adv-modify-page",
  templateUrl: "adv-modify.html",
  styleUrls: ["adv-modify.scss"]
})
export class AdvModifyPage implements OnInit {
  validateForm: FormGroup;
  isSpinning: boolean = false;
  functions: any = [];
  @Input() inValue: any = [];
  @Output() outPut = new EventEmitter();
  //系统类型
  discriptionList: any = Config.SYS_TYPE;//所属类型
  advertisementTypeList: any = Config.ADVERTISEMENT_TYPE;//操作类型
  advertisementBean: AdvertisementBean = new AdvertisementBean();
  informationBean : InformationBean = new InformationBean();
  linkAddr:any = []//链接地址
  infoList: any = []; //资讯详情
  config: any = Config;
  constructor(
    private fb: FormBuilder,
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService
  ) {}

  ngOnInit() {
    this.validateForm = this.fb.group({
      discription: [null, [Validators.required]], //
      operateType: [null, [Validators.nullValidator]], //
      linkUrl: [null, [Validators.nullValidator]], //
      infoId:[null, [Validators.nullValidator]],
      infoTitle:[null, [Validators.nullValidator]],
      fileUrls:[null, [Validators.nullValidator]],

    });
    // 页面初始化时，获取资讯信息
    this.informationBean.pageSize = 1000;
    this.informationBean.currentPage = 1;
    this.httpServcie.getInformationList(this.informationBean).subscribe(data=>{
      this.infoList = data.data.list;
    });

    if (this.inValue) {
      this.advertisementBean = Object.assign({}, this.inValue);
    }
  }


  submitForm()  {
    //没有填的标红
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
    }
    //处理是否提交
    for (const i in this.validateForm.controls) {
      if (!this.validateForm.controls[i].valid) {
        return;
      }
    }
    this.isSpinning = true;
    if (this.fileList.length > 0) {
      let list: any = [];
      this.fileList.forEach(element => {
        list.push(element["response"]["data"]);
      });
      this.advertisementBean.url = list.toString();
    }
    if (this.advertisementBean.operateType=='3') {
      this.advertisementBean.linkUrl = null;
    }
    if (this.advertisementBean.operateType!='3') {
      this.advertisementBean.infoId = null;
    }

    this.httpServcie.advertiseUpdate(this.advertisementBean).subscribe(data => {
      this.isSpinning = false;
      if (data["status"] != "200") {
        this._notification.create("error", "错误提示", "修改失败！");
        //return;
      }
      if (data["status"] === "200") {
        this._notification.create("success", "", "修改成功！");
      }
      this.outPut.emit(true);
    });
  }

  quit() {
    this.outPut.emit(true);
  }
  //文件上传
  previewImage = "";
  previewVisible = false;
  fileList: UploadFile[] = [];

  //查看
  handlePreview = (file: UploadFile) => {
    this.previewImage =
      this.config.IP + this.config.API.getUpload + file["response"]["data"] ||
      file.thumbUrl;
    this.previewVisible = true;
  };

  /**
   * 上传就开始调用
   * @param info
   */
  handleChange(info: { file: UploadFile }) {
    if (info.file.status === "uploading") {
      return;
    }
    if (info.file.status === "done") {
      //设置为接口的查询不然反显有时候没用
      this.fileList.forEach(element => {
        element["thumbUrl"] =
          this.config.IP +
          this.config.API.getUpload +
          element["response"]["data"];
      });
      // Get this url from response in real world.
    }
  }

  //移除文件
  nzRemoveFile = (file: UploadFile) => {
    this.httpServcie.deleteUpload(file["response"]["data"]).subscribe(
      data => {
        if (data["status"] != "200") {
          this._notification.create("error", "错误提示", "删除失败！");
          return;
        }
        //移除fileList中的数据
        if (this.fileList.length == 1) {
          this.fileList = [];
          return;
        }
        let list: UploadFile[] = [];
        this.fileList.forEach((element, index) => {
          if (element["thumbUrl"] == file.thumbUrl) {
            this.fileList.splice(index, 1);
            this.fileList = this.fileList;
            list = this.fileList;
            this.fileList = [];
          }
        });
        list.forEach(element => {
          this.fileList.push(element);
        });
      },
      error => {
        this._notification.create("error", "错误提示", "请求超时！");
        return;
      }
    );
  };
  /**
   * 获取下拉框选中的值
   */
 /* getChange(val) {
    console.log(val.bean.operateType);
  }*/
}
